<?php namespace app\common\service;
use  \app\common\model\AuctionGood as AuctionGoodModel;


/**
 * Class AuctionGood
 * @package app\common\service
 */
class AuctionGood extends Base{

    /**
     * AuctionGood constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $AuctionGoodModel= new AuctionGoodModel();
        $this->model = $AuctionGoodModel;
    }


    /**
     * @param $where
     * @param $page
     * @param int $limit
     * @return array
     */
    public function getList($where, $page, $limit=10){
        try{
            $data=$this->paginatet($where,'*','createtime desc',$limit,$page,'');

            if(!empty($data)){
                foreach ($data as $key=>$val){
                    $val->noewTime=datetime((int)$val->createtime+(int)$val->hour*3600);
                    if(!empty($val->images)){
                        $img=explode(',',$val->images);
                        $val->img=$img[0];
                    }
                }
            }

        }catch (\Exception $e){
            $data=[];
        }
        return $data;
    }

    /**
     * @param $where
     * @param $page
     * @param int $limit
     * @return array
     */
    public function goodList($where, $page, $limit=10){
        try{
            $data=$this->paginatet($where,'*','createtime desc',$limit,$page,'');

            if(!empty($data)){
                foreach ($data as $key=>$val){
                    $val->noewTime=datetime((int)$val->createtime+(int)$val->hour*3600);
                    if(!empty($val->images)){
                        $img=explode(',',$val->images);
                        $val->img=$img[0];
                    }else{
                        $val->img=[];
                    }
                    if($val->status==1){
                        $val->status_name='进行中';
                    }elseif ($val->status==2){
                        $val->status_name='已结束';
                    }elseif ($val->status==3){
                        $val->status_name='竞拍失败';
                    }else{
                        $val->status_name='';
                    }
                    if($val->send==0){
                        $val->send_name='未发货';
                    }elseif ($val->send==1){
                        $val->send_name='已发货';
                    }elseif ($val->send==2){
                        $val->send_name='已收货';
                    }else{
                        $val->send_name='';
                    }

                }
            }

        }catch (\Exception $e){
            $data=[];
        }
        return $data;
    }

    /**
     * 我的参与竞价商品列表
     * @param $where
     * @param $page
     * @param int $limit
     * @return array
     */
    public function participategood($where, $page, $limit=10,$receiverUserId)
    {
        try {
            $data = $this->paginatet($where, '*', 'createtime desc', $limit, $page, '');

            if (!empty($data)) {
                foreach ($data as $key => $val) {
                    $val->noewTime = datetime((int)$val->createtime + (int)$val->hour * 3600);
                    if (!empty($val->images)) {
                        $img = explode(',', $val->images);
                        $val->img = $img[0];
                    }else{
                        $val->img=[];
                    }
                    if ($val->status == 1) {
                        $val->status_name = '进行中';
                    } elseif ($val->status == 2) {
                        $val->status_name = '已结束';
                    } elseif ($val->status == 3) {
                        $val->status_name = '竞拍失败';
                    }else{
                        $val->status_name='';
                    }
                    if ($val->send == 0) {
                        $val->send_name = '未发货';
                    } elseif ($val->send == 1) {
                        $val->send_name = '已发货';
                    } elseif ($val->send == 2) {
                        $val->send_name = '已收货';
                    }else{
                        $val->send_name='';
                    }

                    if ($val->status == 2) {
                        if($val->receiverUserId==$receiverUserId){
                            $val->suc='获拍成功';
                        }else{
                            $val->suc='';
                        }
                    }else{
                        $val->suc='';
                    }
                    if ($val->refund == 1) {
                        $val->refund_name='已退款';
                    }elseif($val->refund == 0) {
                        $val->refund_name='未退款';
                    }else{
                        $val->refund_name='';
                    }

                }
            }

        } catch (\Exception $e) {
            $data = [];
        }
        return $data;
    }


    /**
     * @param $data
     * @return bool|string
     */
    public function add($data)
    {
       try{
           $data= parent::add($data);
       }catch (\Exception $e){
           $data=[];
       }
       return $data;
    }

    /**
     * @param $map
     * @return array
     */
    public function find($map)
    {
        try{
            $data=parent::find($map);
            if(!empty($data)){
                $data->noewTime=datetime((int)$data->createtime+(int)$data->hour*3600);
                if(!empty($data->images)){
                    $data->img=explode(',',$data->images);
                }else{
                    $data->img=[];
                }
                if($data->status==1){
                    $data->status_name='进行中';
                }elseif ($data->status==2){
                    $data->status_name='已结束';
                }elseif ($data->status==3){
                    $data->status_name='竞拍失败';
                }else{
                    $data->status_name='';
                }

            }
        }catch (\Exception $e){
            $data=[];
        }
        return $data;
    }


}